using Engine;
using Engine.Graphics;
using Game;

namespace RuthlessConquest {
    public class CreditsScreen : Screen {
        public ButtonWidget OkButton;

        //public RectangleWidget[] Rectangles = new RectangleWidget[7];
        public ClickableWidget ClickableWidget1;
        public ClickableWidget ClickableWidget2;
        public ClickableWidget ClickableWidget3;
        public const string fName = "RCCreditsScreen";

        public CreditsScreen() {
            Widget[] obj = [new BackgroundWidget(), new BoidsWidget(), new InterlaceWidget(), null];
            StackPanelWidget panel1 = new() {
                Direction = LayoutDirection.Vertical,
                HorizontalAlignment = WidgetAlignment.Center,
                Children = {
                    new CanvasWidget { Size = new Vector2(0f, 10f) },
                    new LabelWidget {
                        Text = LanguageControl.Get(fName, "1"),
                        FontScale = 1.5f,
                        FontSpacing = new Vector2(1f, 0f),
                        Color = Colors.High,
                        HorizontalAlignment = WidgetAlignment.Center
                    },
                    new CanvasWidget { Size = new Vector2(0f, 0f) },
                    new StackPanelWidget {
                        Children = { new LabelWidget { Text = LanguageControl.Get(fName, "2"), FontScale = 0.75f } },
                        HorizontalAlignment = WidgetAlignment.Center
                    },
                    new CanvasWidget { Size = new Vector2(0f, float.PositiveInfinity) }
                }
            };
            UniformSpacingPanelWidget panel2 = new() { Direction = LayoutDirection.Horizontal, HorizontalAlignment = WidgetAlignment.Center };
            CanvasWidget canvasWidget = new();
            ClickableWidget1 = new ClickableWidget { HorizontalAlignment = WidgetAlignment.Stretch, VerticalAlignment = WidgetAlignment.Stretch };
            canvasWidget.Children.Add(ClickableWidget1);
            canvasWidget.Children.Add(
                new StackPanelWidget {
                    Direction = LayoutDirection.Vertical,
                    HorizontalAlignment = WidgetAlignment.Center,
                    Margin = new Vector2(30f, 0f),
                    IsHitTestVisible = false,
                    Children = {
                        new RectangleWidget {
                            Subtexture = new Subtexture(Textures.Gui.SurvivalcraftIcon, Vector2.Zero, Vector2.One),
                            Size = new Vector2(96f, 96f),
                            FillColor = Color.White,
                            OutlineColor = Color.Transparent,
                            HorizontalAlignment = WidgetAlignment.Center
                        },
                        new CanvasWidget { Size = new Vector2(0f, 15f) },
                        new LabelWidget {
                            Text = LanguageControl.Get(fName, "3"),
                            Color = Colors.ForeDim,
                            FontScale = 0.75f,
                            TextAnchor = TextAnchor.Center,
                            HorizontalAlignment = WidgetAlignment.Center
                        }
                    }
                }
            );
            panel2.Children.Add(canvasWidget);
            CanvasWidget canvasWidget2 = new();
            ClickableWidget2 = new ClickableWidget { HorizontalAlignment = WidgetAlignment.Stretch, VerticalAlignment = WidgetAlignment.Stretch };
            canvasWidget2.Children.Add(ClickableWidget2);
            canvasWidget2.Children.Add(
                new StackPanelWidget {
                    Direction = LayoutDirection.Vertical,
                    HorizontalAlignment = WidgetAlignment.Center,
                    Margin = new Vector2(30f, 0f),
                    IsHitTestVisible = false,
                    Children = {
                        new RectangleWidget {
                            Subtexture = new Subtexture(Textures.Gui.Survivalcraft2Icon, Vector2.Zero, Vector2.One),
                            Size = new Vector2(96f, 96f),
                            FillColor = Color.White,
                            OutlineColor = Color.Transparent,
                            HorizontalAlignment = WidgetAlignment.Center
                        },
                        new CanvasWidget { Size = new Vector2(0f, 15f) },
                        new LabelWidget {
                            Text = LanguageControl.Get(fName, "4"),
                            Color = Colors.ForeDim,
                            FontScale = 0.75f,
                            TextAnchor = TextAnchor.Center,
                            HorizontalAlignment = WidgetAlignment.Center
                        }
                    }
                }
            );
            panel2.Children.Add(canvasWidget2);
            CanvasWidget canvasWidget3 = new();
            ClickableWidget3 = new ClickableWidget { HorizontalAlignment = WidgetAlignment.Stretch, VerticalAlignment = WidgetAlignment.Stretch };
            canvasWidget3.Children.Add(ClickableWidget3);
            canvasWidget3.Children.Add(
                new StackPanelWidget {
                    Direction = LayoutDirection.Vertical,
                    HorizontalAlignment = WidgetAlignment.Center,
                    Margin = new Vector2(30f, 0f),
                    IsHitTestVisible = false,
                    Children = {
                        new RectangleWidget {
                            Subtexture = new Subtexture(Textures.Gui.BugsIcon, Vector2.Zero, Vector2.One),
                            Size = new Vector2(96f, 96f),
                            FillColor = Color.White,
                            OutlineColor = Color.Transparent,
                            HorizontalAlignment = WidgetAlignment.Center
                        },
                        new CanvasWidget { Size = new Vector2(0f, 15f) },
                        new LabelWidget {
                            Text = LanguageControl.Get(fName, "5"),
                            Color = Colors.ForeDim,
                            FontScale = 0.75f,
                            TextAnchor = TextAnchor.Center,
                            HorizontalAlignment = WidgetAlignment.Center
                        }
                    }
                }
            );
            panel2.Children.Add(canvasWidget3);
            panel1.Children.Add(panel2);
            panel1.Children.Add(new CanvasWidget { Size = new Vector2(0f, float.PositiveInfinity) });
            panel1.Children.Add(
                new StackPanelWidget {
                    HorizontalAlignment = WidgetAlignment.Center,
                    Children = {
                        new LinkWidget {
                            Text = LanguageControl.Get(fName, "6"), FontScale = 0.75f, Color = Colors.High, Url = "kaalus.wordpress.com"
                        }
                    }
                }
            );
            panel1.Children.Add(new CanvasWidget { Size = new Vector2(0f, float.PositiveInfinity) });
            OkButton = new BevelledButtonWidget {
                Text = LanguageControl.Ok, Size = new Vector2(200f, 60f), HorizontalAlignment = WidgetAlignment.Center
            };
            panel1.Children.Add(OkButton);
            panel1.Children.Add(new CanvasWidget { Size = new Vector2(0f, 5f) });
            obj[3] = panel1;
            Children.Add(obj);
        }

        public override void Update() {
            //for (int i = 0; i < Rectangles.Length; i++) {
            //if (Rectangles[i] != null) {
            //float x = (float)MathUtils.Remainder(Time.RealTime, 10000.0) + i * 1000;
            //Rectangles[i].RenderTransform = Matrix.CreateTranslation(new Vector3(-Rectangles[i].ActualSize / 2f, 0f))
            //* Matrix.CreateRotationZ(6.28f * SimplexNoise.OctavedNoise(x, 0.25f, 4, 2f, 0.5f) + 3.14f)
            //* Matrix.CreateTranslation(new Vector3(Rectangles[i].ActualSize / 2f, 0f));
            //}
            //}
            if (ClickableWidget1 != null
                && ClickableWidget1.IsClicked) {
                switch (VersionsManager.Platform) {
                    case Platform.Ios: WebBrowserManager.LaunchBrowser("https://apps.apple.com/us/app/survivalcraft-day-one/id1068326506"); break;
                    case Platform.Amazon: WebBrowserManager.LaunchBrowser("https://www.amazon.com/dp/B009ZXT4BY"); break;
                    default:
                        WebBrowserManager.LaunchBrowser("https://play.google.com/store/apps/details?id=com.candyrufusgames.survivalcrafttrial");
                        break;
                }
            }
            if (ClickableWidget2 != null
                && ClickableWidget2.IsClicked) {
                switch (VersionsManager.Platform) {
                    case Platform.Desktop:
                    case Platform.Windows81:
                    case Platform.WindowsPhone81:
                    case Platform.Steam: WebBrowserManager.LaunchBrowser("https://www.microsoft.com/p/survivalcraft-2/9phc48p58nb2"); break;
                    case Platform.Android:
                        WebBrowserManager.LaunchBrowser("https://play.google.com/store/apps/details?id=com.candyrufusgames.survivalcraft2"); break;
                    case Platform.Ios: WebBrowserManager.LaunchBrowser("https://apps.apple.com/us/app/survivalcraft-2/id1185580782"); break;
                    case Platform.Amazon:
                        WebBrowserManager.LaunchBrowser("http://www.amazon.com/gp/mas/dl/android?p=com.candyrufusgames.survivalcraft2a"); break;
                }
            }
            if (ClickableWidget3 != null
                && ClickableWidget3.IsClicked) {
                switch (VersionsManager.Platform) {
                    case Platform.Desktop:
                    case Platform.Windows81:
                    case Platform.WindowsPhone81:
                    case Platform.Steam: WebBrowserManager.LaunchBrowser("https://www.microsoft.com/store/apps/9pnpzkcmvhlf"); break;
                    case Platform.Android:
                        WebBrowserManager.LaunchBrowser("https://play.google.com/store/apps/details?id=com.candyrufusgames.bugs"); break;
                    case Platform.Ios: WebBrowserManager.LaunchBrowser("https://itunes.apple.com/app/bugs/id1190979375"); break;
                    case Platform.Amazon:
                        WebBrowserManager.LaunchBrowser("https://www.amazon.com/gp/mas/dl/android?p=com.candyrufusgames.bugs"); break;
                }
            }
            if (Input.Back
                || OkButton.IsClicked) {
                ScreensManager.SwitchScreen("RCMainMenu");
            }
        }
    }
}